<template>
  <div>
    <el-form ref="form" label-width="130px" :model="form" :rules="rules">
      <el-form-item label="用户名">{{ info.loginName }}</el-form-item>
      <el-form-item label="真实姓名">{{ info.lastName }}</el-form-item>
      <el-form-item label="手机号">{{ info.mobile }}</el-form-item>
      <el-form-item label="原密码" prop="originalPassword">
        <el-col :span="8"><el-input v-model="form.originalPassword" type="password" placeholder="请输入原始密码"></el-input></el-col>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-col :span="8"><el-input v-model="form.password" type="password" placeholder="请输入新密码"></el-input></el-col>
      </el-form-item>
      <el-form-item label="谷歌验证码" prop="googleCode">
        <el-col :span="8"><el-input v-model="form.googleCode" placeholder="请输入谷歌验证码"></el-input></el-col>
      </el-form-item>

      <el-form-item><el-button type="primary" @click="modifypass">确定</el-button></el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getById, updatepass } from '@/api/sys/admin';
export default {
  data() {

    return {
      info: {},
      form: {
        adminId: '',
        googleCode: '',
        originalPassword: '',
        password: ''
      },
      rules: {
        originalPassword: [{ required: true, message: '请填写原密码', trigger: 'blur' }],
        password: [{ required: true, message: '请填写新密码', trigger: 'blur' }],
        googleCode: [{ required: true, message: '请填写google验证码', trigger: 'blur' }]
      }
    };
  },
  created() {
    const adminId = this.$store.getters.accountId;
    this.form.adminId = adminId;
    this.getinfo(adminId);
  },
  methods: {
    getinfo(adminId) {
      getById({ adminId: adminId }).then(res => {
        this.info = res.result;
      });
    },
    modifypass() {
      // 校验
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 校验成功
          this.$confirm(`确定修改密码？`, '提示', {
            type: 'warning',
            beforeClose: (action, instance, done) => {
              if (action === 'confirm') {
                instance.confirmButtonLoading = true;
                updatepass(this.form)
                  .then(res => {
                    instance.confirmButtonLoading = false;

                    done();
                    this.$message({
                      type: 'success',
                      message: res.message
                    });
                  })
                  .catch(() => {
                    instance.confirmButtonLoading = false;
                     done()
                  });
              } else {
                done();
              }
            }
          });
        } else {
          return;
        }
      });
    }
  }
};
</script>

<style></style>
